﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnhancedWebPart;
using System.Web.UI.WebControls;
using System.ComponentModel.Composition;

namespace EnhancedWebpartProject.EnhancedVisualWebPart
{
    [Export(typeof(IDisplayableClass))]
    public class DisplayableFilterSetting : IDisplayableClass
    {

        public bool IsAppliable(Type propertyType)
        {
            return propertyType == typeof(FilterSetting);
        }

        public bool IsControlInHeaderSection()
        {
            return false;
        }

        public System.Web.UI.Control CreateControl()
        {
            Panel panel = new Panel();
            DropDownList ddl = new DropDownList();
            ddl.ID = "FilterSettingDropDownList";
            ddl.Items.Add("Contains");
            ddl.Items.Add("Equals");
            panel.Controls.Add(ddl);
            TextBox txt = new TextBox();
            txt.ID = "FilterSettingTextBox";
            panel.Controls.Add(txt);

            return panel;
        }

        public void SetControlValue(System.Web.UI.Control control, object value)
        {
            Panel panel = (Panel)control;
            DropDownList ddl = (DropDownList)panel.Controls[0];
            TextBox txt = (TextBox)panel.Controls[1];

            FilterSetting filterSetting = (FilterSetting)value;

            if (filterSetting == null)
                filterSetting = new FilterSetting();

            txt.Text = filterSetting.Value;
            ddl.SelectedIndex = (int)filterSetting.Type;
        }

        public object GetControlValue(System.Web.UI.Control control)
        {
            Panel panel = (Panel)control;
            DropDownList ddl = (DropDownList)panel.Controls[0];
            TextBox txt = (TextBox)panel.Controls[1];

            FilterSetting filterSetting = new FilterSetting();
            filterSetting.Type = (FilterType)ddl.SelectedIndex;
            filterSetting.Value = txt.Text;

            return filterSetting;
        }
    }
}
